package com.monitoring.management.common.File;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created by liujunnan on 2017/7/16.
 */
public class UploadFileUtils {
    public static List uploadFile(long max_size, HttpServletRequest request, String fileLocalPath) throws Exception {
        List fileList = new ArrayList();
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                List<MultipartFile> files = multiRequest.getFiles(iter.next());
                for (int i = 0; i < files.size(); i++) {
                    MultipartFile file = files.get(i);
                    String myFileName = file.getOriginalFilename();
                    if (StringUtils.notEmpty(myFileName)) {
                        Map map = new HashMap();
                        String type = getFileType(myFileName);
                        String fileName = get32UUID() + type;
                        InputStream input = file.getInputStream();
                        //本地用于存放文件的路径
                        String localPath = request.getSession().getServletContext().getRealPath("/") + fileLocalPath + fileName;
                        FileOutputStream os = new FileOutputStream(localPath);
                        byte[] buffer = new byte[1024];
                        int n = 0;
                        while (-1 != (n = input.read(buffer))) {
                            os.write(buffer, 0, n);
                        }
                        os.flush();
                        os.close();
                        input.close();
                        map.put("localPath", localPath);
                        map.put("file_url", fileName);
                        fileList.add(map);
                    }
                }
            }
        }
        return fileList;
    }
    private static String getFileType(String file_name) {
        int length = file_name.length();
        int potin = file_name.lastIndexOf(".");
        String file_type = "";
        if (potin == -1) {
            if (length > 6) {
                potin = length - 5;
            } else {
                potin = length - 3;
            }
        }
        file_type = file_name.substring(potin, length).toLowerCase();
        return file_type;
    }
    public static String get32UUID() {
        return UUID.randomUUID().toString().trim().replaceAll("-", "");
    }
    public static void deleteFile(String localPath) {
        File fileLocal = new File(localPath);
        fileLocal.delete();
    }
}